package com.demo;

import java.io.File;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**filter
 * @author Ziroom
 *
 */
public class DemoFilter {

	private static final String PATH = "d:\\zsl\\index8";

	public static void createIndex() throws Exception {
        //1、创建一个Director对象，指定索引库保存的位置。

        //1.1把索引库保存在内存中
        //Directory directory = new RAMDirectory();

        //1.2把索引库保存在磁盘
        Directory directory = FSDirectory.open(new File(PATH).toPath());

        //2、基于Directory对象创建一个IndexWriter对象
        IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer());
        //当使用IKAnalyzer分词时，是如下写法。
       	//IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer());
        IndexWriter indexWriter = new IndexWriter(directory, config);

        //3、读取磁盘上的文件，对应每个文件创建一个文档对象。
        String[] arr = new String[] {
        	"lucene lucene lucene lucene lucene lucene action",
        	"lucene"
        };
        int i=0;
        for (String str : arr) {
            //取文件名
            String fileName = "测试文件" + i++;
            //String fileName = "lucene"; // 就算内容都是lucene，因为是不同的field，所以在ByteBlockPool也是要存两份的
            //文件的内容
            String fileContent = str;
            //文件的大小
            long fileSize = str.length();

            //创建Field
            //参数1：域的名称，参数2：域的内容，参数3：是否存储
            Field fieldName = new StringField("name", fileName, Field.Store.YES);
            Field fieldContent = new TextField("content", fileContent, Field.Store.YES);

            //创建文档对象
            Document document = new Document();
            //向文档对象中添加域
            document.add(fieldName);
            document.add(fieldContent);

            //5、把文档对象写入索引库
            System.out.println(indexWriter.addDocument(document));

        }
        //6、关闭indexwriter对象
        indexWriter.close();
    }

	public static void searchIndex() throws Exception {
        //1、创建一个Director对象，指定索引库的位置
        Directory directory = FSDirectory.open(new File(PATH).toPath());
        //2、创建一个IndexReader对象
        IndexReader indexReader = DirectoryReader.open(directory);
        //3、创建一个IndexSearcher对象，构造方法中的参数indexReader对象。
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        //4、创建一个Query对象，TermQuery
        Query query = new TermQuery(new Term("content", "action"));
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        // booleanQuery中使用filter查询也可以让该部分的查询结果不参与打分
        builder.add(query, BooleanClause.Occur.FILTER);
        BooleanQuery booleanQuery = builder.build();
        //5、执行查询，得到一个TopDocs对象
        //参数1：查询对象 参数2：查询结果返回的最大记录数
        TopDocs topDocs = indexSearcher.search(booleanQuery, 10);
        //6、取查询结果的总记录数
        System.out.println("查询总记录数：" + topDocs.totalHits);
        //7、取文档列表
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        //8、打印文档中的内容
        for (ScoreDoc doc : scoreDocs) {
            //取文档id
            int docId = doc.doc;
            //根据id取文档对象
            Document document = indexSearcher.doc(docId);
            System.out.println(docId);
            System.out.println(document.get("name"));
            System.out.println(document.get("content"));

        }
        //9、关闭IndexReader对象
        indexReader.close();
    }

	public static void main(String[] args) throws Exception {
		//createIndex();
		for(int i=0; i<257; i++) {
			searchIndex();
		}
	}

}
